package org.yly.game.rpggame.domain.model.task;

import java.util.HashSet;
import java.util.Set;

import org.yly.game.rpggame.domain.model.DomainEventPublisher;
import org.yly.game.rpggame.domain.model.task.condition.GetGoodsCompleteCondition;
import org.yly.game.rpggame.domain.model.task.condition.KillMonsterCompleteCondition;
import org.yly.game.rpggame.domain.model.task.condition.TaskCompleteCondition;

/**
 * 玩家接受后的任务
 * 
 * @author makefu
 * 
 */
public class AcceptedTask {

	private Task task;
	private Set<TaskCompleteCondition> completeConditionSet;
	private TaskState taskState = TaskState.Accept;

	public AcceptedTask(Task task) {
		this.task = task;
		
		this.completeConditionSet = new HashSet<TaskCompleteCondition>();
		completeConditionSet.add(new GetGoodsCompleteCondition(this, task.getNeedGoodsMap()));
		completeConditionSet.add(new KillMonsterCompleteCondition(this, task.getNeedKillMonsterIdMap()));
	}

	public Task getTask() {
		return task;
	}

	public TaskState getTaskState() {
		return taskState;
	}

	/**
	 * 提交任务，随机获得奖励
	 * 
	 * @return
	 */
	public void submit() {
		this.taskState = TaskState.Complete;
	}
	
	/**
	 * 验证任务是否完成
	 * 
	 * @return
	 */
	public boolean checkCompleteCondition() {
		boolean isOk = true;
		for (TaskCompleteCondition req : completeConditionSet) {
			isOk = isOk && req.check();
		}
		if (isOk) {
			DomainEventPublisher.getInstance().publish(new TaskCompleteEvent(this.task.id()));
		}
		return isOk;
	}

}
